package com.os.opencv.java.chapter11;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Scalar;
import org.opencv.features2d.Features2d;
import org.opencv.features2d.ORB;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.nio.file.Path;

public class Orb {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        //读取图像并在屏幕上显示
        Mat src = Imgcodecs.imread("pics/goChild.png", Imgproc.COLOR_BGR2GRAY);
        Mat gray = new Mat();
        Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY);
        HighGui.imshow("gray", gray);
        HighGui.waitKey(0);

        //用orb算法检测特征点
        int nfeatures = 200;  //特征点的数量
        float scaleFactor = 1.2f;  //金字塔缩放比例
        int nlevels = 8;  //金字塔层数
        int edgeThreshold = 31;  //边缘阈值
        int firstLevel = 0;  //原图像在金字塔的第几层
        int WET_K = 2;  //brief描述子需要像素数
        int scoreType = ORB.HARRIS_SCORE;  //排序算法
        int patchSize = 31;  //领域大小
        int fastThreshold = 20;  //fast角点像素差值的阈值
        ORB orb = ORB.create(nfeatures, scaleFactor, nlevels, edgeThreshold, firstLevel, WET_K, scoreType, patchSize, fastThreshold);

        MatOfKeyPoint pts = new MatOfKeyPoint();
        orb.detect(gray, pts);

        //检测出特征点
        Mat dst = new Mat();
        Scalar red = new Scalar(0,0,255);
        int flags = Features2d.DrawMatchesFlags_DRAW_RICH_KEYPOINTS;

        Features2d.drawKeypoints(src, pts, dst, red, flags);

        //在屏幕上显示出检测出的特征点
        HighGui.imshow("orb", dst);
        HighGui.waitKey(0);
        System.exit(0);
    }
}
